package com.tn.wservice.validator;

import java.util.HashMap;

import com.tn.service.ContextService;
import com.tn.wservice.IYSNWebService;

@SuppressWarnings("unchecked")
public class ValidationService implements IYSNWebService {

	private static HashMap<Class, ValidationEngine> validatorMap = null;

	// Initialise once on load
	static {
		validatorMap = (HashMap<Class, ValidationEngine>) ContextService
				.getInstance().getBean("validationEngineMap");
	}

	public static IValidationResult validate(Object businessObj)
			throws Exception {
		ValidationEngine engine = getValidationEngine(businessObj.getClass());
		IValidationResult result = engine.applyValidations(businessObj);
		return result;
	}

	public static ValidationEngine getValidationEngine(Class boClass) {
		return validatorMap.get(boClass.getName());
	}

	public static boolean isValidEmailAddress(String userName) {
		return userName.matches(EMAIL_PATTERN);
	}

	public static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

	public static void main(String[] args) {
		System.out.println(isValidEmailAddress("vinay123@gmail.com\n"));
	}
	
}
